package cz.muni.fi.pb138.videoweb2013.servlets;

import cz.muni.fi.pb138.videoweb2013.backend.ODFTransformation;
import cz.muni.fi.pb138.videoweb2013.backend.ODFTransformationImpl;
import java.io.BufferedInputStream;
import java.io.IOException;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * import from ods format action
 *
 * @author Filip
 */
@UrlBinding("/action/import")
public class ImportActionBean implements ActionBean{
    private ActionBeanContext context;
    private FileBean importFile;
    private boolean sign = false;

    public FileBean getImportFile() {
        return importFile;
    }

    public void setImportFile(FileBean importFile) {
        this.importFile = importFile;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    public boolean getSign(){
        return sign;
    }
    
    public void setSign(boolean sign){
        this.sign = sign;
    }

    /**
     * imports ods format from user and saves all mediums from it into database
     * 
     * @return forward resolution to page ImportFinished or to ImportNoFile if no file is selected
     * @throws IOException 
     */
    @DefaultHandler
    public Resolution importODF() throws IOException {
        if (importFile != null) {
            ODFTransformation odf = new ODFTransformationImpl();
            BufferedInputStream bis = new BufferedInputStream(importFile.getInputStream());
            odf.importODF(bis);
            sign = false;
            return new ForwardResolution("/ImportFinished.jsp");
        }else{
            sign = true;
            return new ForwardResolution("/ImportNoFile.jsp");
        }
    }
    
}
